/*
 * author:       可乐加糖
 * email:        black.angel.liu@gmail.com
 * des:          游戏主体内容
 */

var ctx = document.getElementById("gamecanvas").getContext("2d");
var cwidth = 400,cheight=300,dicex=50,dicey=50,dicewidth=100,diceheight=100,dotrad=6;

function init(){
	window.setInterval(function(){
		var _tempNumber_ = Math.floor(1+Math.random()*6);
		drawface(_tempNumber_);
	},100)
}
function throwdice(){
	var ch = 1+Math.floor(Math.random()*6);
	dx = dicex;
	dy = dicey;
	drawface(ch);
	dx = dicex + 150;
	ch = 1 + Math.floor(Math.random()*6);
	drawface(ch);
}
function drawface(n){
	ctx.lineWidth = 5;
	ctx.clearRect(dicex,dicey,dicewidth,diceheight);
	ctx.strokeStyle = "#ccc";
	ctx.strokeRect(dicex,dicey,dicewidth,diceheight);
	ctx.fillStyle = "#ff0000";
	switch(n){
		case 1:
			draw1();
			break;
		case 2:
			draw2();
			break;
		case 3:
			draw2();
			draw1();
			break;
		case 4:
			draw4();
			break;
		case 5:
			draw4();
			draw1();
			break;
		case 6:
			draw4();
			draw2mid();
			break;
	}
}

function draw1(){
	var dotx,doty;
	ctx.beginPath();
	dotx = dicex + .5*dicewidth;
	doty = dicey + .5*diceheight;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	ctx.closePath();
	ctx.fill();
}

function draw2(){
	var dotx,doty;
	ctx.beginPath();
	dotx = dicex + 3*dotrad;
	doty = dicey + 3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	dotx = dicex+dicewidth-3*dotrad;
	dotx = dicey+dicewidth-3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	ctx.closePath();
	ctx.fill();
}
function draw4(){
	var dotx,doty;
	ctx.beginPath();
	dotx = dicex + 3*dotrad;
	doty = dicey + 3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	dotx = dicex+dicewidth-3*dotrad;
	doty = dicey+diceheight-3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	ctx.closePath();
	ctx.fill();
	ctx.beginPath();
	dotx = dicex+3*dotrad;
	doty = dicey+diceheight-3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	dotx = dicex+dicewidth-3*dotrad;
	doty = dicey+3*dotrad;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	ctx.closePath();
	ctx.fill();
}
function draw2mid(){
	var dotx,doty;
	ctx.beginPath();
	dotx = dicex + 3*dotrad;
	doty = dicey + .5*diceheight;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	dotx = dicex+dicewidth-3*dotrad;
	doty = dicey+.5*diceheight;
	ctx.arc(dotx,doty,dotrad,0,Math.PI*2,true);
	ctx.closePath();
	ctx.fill();
}
window.onload = init();
